/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hotelsupplymanagement;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.beans.*;
import java.util.*;

class AddProduct {

    JLabel l1,l2,l3,l4,l5;
    JTextField t1,t2,t3,t4;
    JTextArea t5;
    JButton jbutton1,jbutton2;
    JPanel jpanel1,jpanel2;
    FileOutputStream fos;
    PrintStream ps;

    public void addProduct()
	{
            JFrame jf=new JFrame("Addition of a New Product");
            jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            jf.setBackground(Color.white);
            jf.setSize(400,300);
            jf.setLocation(250,200);
            jf.setVisible(true);
            jf.setLayout(new BorderLayout(30,30));
            JLabel jl=new JLabel("Enter the following details of the product");
            jpanel1=new JPanel();
            jpanel1.setLayout(new GridLayout(1,1));
            jpanel1.add(jl);
            jf.add(jpanel1,BorderLayout.NORTH);
            jpanel2=new JPanel();
            jpanel2.setLayout(new GridLayout(6,2,10,10));


            l1=new JLabel("Name of the Product:");
            jpanel2.add(l1);
            t1=new JTextField();
            jpanel2.add(t1);
            l2=new JLabel("Unique ID of the Product:");
            jpanel2.add(l2);
            t2=new JTextField();
            jpanel2.add(t2);
            l3=new JLabel("Quality of the Product(Rate 1-5):");
            jpanel2.add(l3);
            t3=new JTextField();
            jpanel2.add(t3);
            l4=new JLabel("Product Description(Veg/Meat) :" );
            jpanel2.add(l4);
            t4=new JTextField();
            jpanel2.add(t4);
            l5=new JLabel("Name of Inventories:(;)");
            jpanel2.add(l5);
            t5=new JTextArea();
            JScrollPane areaScrollPane = new JScrollPane(t5);
            jpanel2.add(areaScrollPane);
            jbutton1=new JButton("Add");
            jpanel2.add(jbutton1);
            jbutton2=new JButton("Clear");
            jpanel2.add(jbutton2);
            jf.add(jpanel2,BorderLayout.CENTER);

	jbutton1.addActionListener(new ActionListener()
        {
	public void actionPerformed(ActionEvent e)
	{
                try {
                        productFileSaving();
                    }
                catch (FileNotFoundException ex)
                    {
                        Logger.getLogger(AddProduct.class.getName()).log(Level.SEVERE, null, ex);
                    }
                catch (IOException ex)
                    {
                        Logger.getLogger(AddProduct.class.getName()).log(Level.SEVERE, null, ex);
                    }

                        clear();
	}
        });
	jbutton2.addActionListener(new ActionListener()
        {
        public void actionPerformed(ActionEvent e)
        {
                clear();
            }
        });

	}

    private void productFileSaving() throws FileNotFoundException, IOException
        {
                GetProducts p = new GetProducts();
                fos=new FileOutputStream("GUIProduct.txt",true);
                ps=new PrintStream(fos);
                p.setProductName(t1.getText());
                p.setProductId(Integer.parseInt(t2.getText()));
                p.setProductQuality(Integer.parseInt(t3.getText()));
                p.setProductDescription(t4.getText());
                p.setProductInvList(t5.getText());
                ps.println(p.getProductName()+","+p.getProductId()+","+p.getProductQuality()+","+p.getProductDescription()+","+p.getProductInvList());
                XMLEncoder e = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("GUIProduct.xml")));
                e.writeObject(p);
                e.close();
                fos.close();
         }


    public void clear()
        {
                t1.setText("");
                t2.setText("");
                t3.setText("");
                t4.setText("");
                t5.setText("");
                return;
        }
}
